#ifndef _CRYPTO_H
#define _CRYPTO_H

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <openssl/ec.h>
#include "key_cert.h"

#define KEY_ID_SIZE              16
#define MS_ENC_SIZE              128
#define VM_ID_SIZE               16
#define VM_VERSION_SIZE          16
#define USER_DATA_SIZE           64
#define SESSION_SIG_SIZE         64
#define TEK_TIK_LEN              32
#define HASH_BLOCK_LEN           32


/* copied from GmSSL */
struct SM2CiphertextValue_st {
    BIGNUM *xCoordinate;
    BIGNUM *yCoordinate;
    ASN1_OCTET_STRING *hash;
    ASN1_OCTET_STRING *ciphertext;
};



typedef uint8_t aes_128_key[128 / 8];
typedef uint8_t sm4_128_key[128 / 8];
typedef uint8_t hmac_key_128[128 / 8];
typedef uint8_t hmac_key_256[256 / 8];
typedef uint8_t hmac_sha_256[256 / 8];
typedef uint8_t hmac_sha_512[512 / 8];
typedef uint8_t nonce_128[128 / 8];
typedef uint8_t nonce_256[256 / 8];
typedef uint8_t iv_128[128 / 8];
typedef unsigned char sha_block[HASH_BLOCK_LEN];


void gen_random_bytes(void* bytes, size_t num_bytes);

int kdf_gen_key(unsigned char* key, unsigned int key_size,
                unsigned char* label, unsigned int label_size,
                unsigned char* context, unsigned int context_size,
                unsigned char* result, unsigned int result_size);

int sm4_ctr128_encrypt(const unsigned char *enckey, const unsigned char *enciv,
                    const unsigned char *input_buf, int input_len,
                    unsigned char *output_buf);

int sm2_pubkey_coords_to_ec_key(ecc_point * coord, EC_KEY **pkey);

int get_public_key_in_cert(csv_cert *cert, ecc_point* point);

EC_KEY* get_sm2_key_pair(char * file, sm2_pubkey* pubkey, unsigned char* priv_key);

int fill_pubkey_in_cert(csv_cert *cert, sm2_pubkey *pubkey);

#endif
